<?php
// 测试菜品信息获取API

// 模拟API请求
function testDishInfoAPI($dishId) {
    $url = 'http://localhost/api/food_ingredient/getDishInfo';
    $data = ['dish_id' => $dishId];
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/x-www-form-urlencoded'
    ]);
    
    $response = curl_exec($ch);
    curl_close($ch);
    
    return $response;
}

// 测试ID为1的菜品
$result = testDishInfoAPI(1);
echo "API Response:\n";
echo $result . "\n";

// 解析JSON响应
$responseData = json_decode($result, true);
if ($responseData && $responseData['code'] == 1) {
    echo "\n解析结果:\n";
    echo "烹饪时长: " . ($responseData['data']['cook_time'] ?? '未知') . "\n";
    echo "制作步骤: " . ($responseData['data']['steps'] ?? '未知') . "\n";
    echo "食材列表:\n";
    
    if (!empty($responseData['data']['ingredients'])) {
        foreach ($responseData['data']['ingredients'] as $ingredient) {
            echo "- " . ($ingredient['name'] ?? '未知') . ": " . ($ingredient['quantity'] ?? '未知') . "\n";
        }
    }
} else {
    echo "\nAPI调用失败: " . ($responseData['msg'] ?? '未知错误') . "\n";
}
?>